package com.boot.util;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;

/**
 *
 * @author  wwj
 *
 */
public class CommonUtils {

    private static final Logger logger = LoggerFactory.getLogger(CommonUtils.class);

    private static final String REQUEST_METHOD_GET = "GET";
    private static final String REQUEST_TMETHOD_POST = "POST";

    private static Pattern pattern = Pattern.compile("^(([1-9]{1}\\d*)|([0]{1}))(\\.(\\d){0,2})?$");

    /**
     * 校验正则
     * 
     * @param regularExpression
     *            正則表達式
     * @param test
     *            需要校驗的值
     */
    public static boolean checkCompile(String regularExpression, String test) {
        Pattern pattern = Pattern.compile(regularExpression, Pattern.CASE_INSENSITIVE);
        return pattern.matcher(test).matches();
    }

    /**
     * 校验正则
     *
     * @param regularExpression
     *            正則表達式
     * @param test
     *            需要校驗的值
     */
    public static boolean checkCompiles(String regularExpression, String test) {
        Pattern pattern = Pattern.compile(regularExpression);
        return pattern.matcher(test).matches();
    }

    /**
     * 描述:获取 post 请求的 byte[] 数组
     * 
     * <pre>
     * 举例：
     * </pre>
     * @param request
     * @return
     * @throws IOException
     */
    private static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
        int contentLength = request.getContentLength();
        if (contentLength < 0) {
            return null;
        }
        byte[] buffer = new byte[contentLength];
        for (int i = 0; i < contentLength;) {

            int readlen = request.getInputStream().read(buffer, i, contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        return buffer;
    }

    /**
     * 描述:获取 post 请求内容
     * 
     * <pre>
     * 举例：
     * </pre>
     * 
     * @param request
     * @return
     * @throws IOException
     */
    private static String getRequestPostJson(HttpServletRequest request) {
        try {
            byte[] buffer = getRequestPostBytes(request);
            String charEncoding = request.getCharacterEncoding();
            if (charEncoding == null) {
                charEncoding = "UTF-8";
            }
            return new String(buffer, charEncoding);
        } catch (Exception e) {
            logger.error("转换异常", e);
        }
        return "";
    }

    /**
     * 
     * @Author: chenguifeng 
     * @Title: getRequestGetJson  
     * @Description: 获取 get 请求内容 ,转换为json格式字符串
     * @param request
     * @return
     */
    private static String getRequestGetJson(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>(10);
        Enumeration<String> parameterNames = request.getParameterNames();
        if(parameterNames.hasMoreElements()) {
            String element = parameterNames.nextElement();
            map.put(element, request.getParameter(element));
        }
        String json = JSON.toJSONString(map);
        return json;
    }

    /**
     * 
     * @Author: chenguifeng
     * @Title: getRequestData
     * @Description: 获取请求内容
     * @param request
     * @return
     */
    public static String getRequestData(HttpServletRequest request) {
        String method = request.getMethod();
        if (REQUEST_METHOD_GET.equals(method)) {
            return getRequestGetJson(request);
        }
        if (REQUEST_TMETHOD_POST.equals(method)) {
            return getRequestPostJson(request);
        }
        return "";
    }


}
